<template>
  <page-header-wrapper :title="false">
    <a-card title="组员合同管理">
      <a-tabs type="card" @change="onChange">
        <a-tab-pane v-for="(item,index) in tabOpts" :key="index" :tab="item"></a-tab-pane>
      </a-tabs>
      <div class="table-page-search-wrapper">
        <a-form-model :model="queryParam" layout="inline">
          <a-row :gutter="48">
            <a-col :md="6" :sm="24">
              <a-form-model-item label="合同编号">
                <a-input v-model="queryParam['filter[contract_no]']" placeholder="输入合同编号"/>
              </a-form-model-item>
            </a-col>
            <a-col :md="6" :sm="24">
              <a-form-model-item label="联系电话">
                <a-input v-model="queryParam['filter[phone]']" placeholder="输入联系电话"/>
              </a-form-model-item>
            </a-col>
            <template v-if="advanced">
              <a-col :md="6" :sm="24">
                <a-form-model-item label="创建时间">
                  <a-range-picker @change="onChange0" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-model-item label="审核时间">
                  <a-range-picker @change="onChange1" />
                </a-form-model-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="顾问姓名">
<!--                  <a-input v-model="queryParam['filter[adviser_name]']" placeholder="输入顾问姓名"/>-->
                  <a-select show-search v-model="queryParam['filter[adviser_name]']" :filter-option="filterOption" placeholder="选择顾问姓名">
                    <a-select-option v-for="(item,index) in adviserOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="客户来源">
                  <a-select show-search v-model="queryParam['filter[source]']" :filter-option="filterOption" placeholder="选择客户来源">
                    <a-select-option v-for="(item,index) in sourceOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
              <a-col :md="6" :sm="24">
                <a-form-item label="咨询类型">
                  <a-select show-search v-model="queryParam['filter[first_type]']" :filter-option="filterOption" placeholder="选择咨询类型">
                    <a-select-option v-for="(item,index) in firstTypeOpts" :key="index" :value="index">{{item}}</a-select-option>
                  </a-select>
                </a-form-item>
              </a-col>
            </template>
            <a-col :md="!advanced && 8 || 24" :sm="24">
              <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
                <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
                <a-button style="margin-left: 8px" @click="resetSearch">重置</a-button>
                <a @click="toggleAdvanced" style="margin-left: 8px">
                  {{ advanced ? '收起' : '展开' }}
                  <a-icon :type="advanced ? 'up' : 'down'"/>
                </a>
              </span>
            </a-col>
          </a-row>
        </a-form-model>
      </div>
      <div class="contract_content">
        <s-table
          bordered
          ref="table"
          :row-key="(record) => record.contract_id"
          :columns="contractColumns"
          :data="contractData"
        >
          <div
            slot="filterDropdown"
            slot-scope="{ setSelectedKeys, selectedKeys, confirm, clearFilters, column }"
            style="padding: 8px"
          >
            <a-input
              v-ant-ref="c => (searchInput = c)"
              :placeholder="`搜索${column.title}`"
              :value="selectedKeys[0]"
              style="width: 188px; margin-bottom: 8px; display: block;"
              @change="e => setSelectedKeys(e.target.value ? [e.target.value] : [])"
              @pressEnter="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            />
            <a-button
              type="primary"
              icon="search"
              size="small"
              style="width: 90px; margin-right: 8px"
              @click="() => handleSearch(selectedKeys, confirm, column.dataIndex)"
            >
              搜索
            </a-button>
            <a-button size="small" style="width: 90px" @click="() => handleReset(clearFilters, column.dataIndex)">
              重置
            </a-button>
          </div>
          <template slot="source" slot-scope="text">
            {{sourceOpts[text]}}
          </template>
          <template slot="first_type" slot-scope="text">
            {{firstTypeOpts[text]}}
          </template>
          <template slot="adviser_name" slot-scope="text">
            <a-tag color="red">{{text}}</a-tag>
          </template>
          <template slot="phone" slot-scope="text">
            {{text}}
<!--            <a-tooltip placement="topLeft" :title="record.all_phone">-->
<!--              <a-button class="copy" :data-clipboard-text="record.all_phone" size="small" @click="copy">{{text}}</a-button>-->
<!--            </a-tooltip>-->
          </template>
          <template slot="amount" slot-scope="text,record">
            <a-tag color="red">总金额：{{text}}</a-tag><br>
            <a-tag color="red">官费：{{record.official_fee}}</a-tag><br>
            <a-tag color="red">代理费：{{record.agency_fee}}</a-tag>
          </template>
          <template slot="tail_money" slot-scope="text">
            <a-tag color="red">{{text == '0.00' ? '无' : text}}</a-tag>
          </template>
          <template slot="status" slot-scope="text">
            <a-tag color="red">{{statusOpts[text]}}</a-tag>
            <div>请进入详情页审核</div>
<!--            <a-button size="small" type="primary" v-if="text == 20 || text == 10" @click="checkContract(record.contract_id)">{{statusOpts[text]}} <a-icon type="form" /></a-button>-->
<!--            <a-button size="small" type="danger" v-else-if="text == 32 || text == 42" @click="checkContract(record.contract_id)">{{statusOpts[text]}} <a-icon type="exclamation-circle" /></a-button>-->
<!--            <a-button size="small" type="primary" v-else @click="checkContract(record.contract_id)" ghost>{{statusOpts[text]}}</a-button>-->
<!--            <a-tag>{{statusOpts[text]}}</a-tag>-->
          </template>
          <template slot="operate" slot-scope="text, record">
            <a-button type="primary" size="small" @click="$router.push({ name: 'ContractDetail', params: { id: record.contract_id } })">查看</a-button>
<!--            <a-button type="danger" size="small">删除</a-button>-->
          </template>
        </s-table>
      </div>
    </a-card>
    <a-modal
      title="编辑合同状态"
      v-model="statusVisible"
      @ok="onCheck"
    >
      <a-form-model
        :model="contractForm"
      >
        <a-form-model-item>
          <a-radio-group v-model="contractForm.status">
            <a-radio v-for="(item,index) in statusRadio" :key="index" :value="index">
              {{item}}
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item>
          <a-textarea v-model="contractForm.notes" placeholder="备注,此外，审核不通过必须填写原因" :rows="4" />
        </a-form-model-item>
      </a-form-model>
    </a-modal>
  </page-header-wrapper>
</template>

<script>
  import { STable } from '@/components'
  import { getContractList, getEditContract, postEditContract } from '@/api/contract'
  import col from '@/columns/contract'
  import Clipboard from 'clipboard'
  const contractColumns = col.all
  export default {
    components: {
      STable
    },
    name: 'ContractList',
    data () {
      return {
        contractColumns,
        contractData: parameters => {
          return getContractList(Object.assign(parameters, this.queryParam)).then(res => {
            this.tabOpts = res.result.tab_opts
            this.firstTypeOpts = res.result.first_type_opts
            this.sourceOpts = res.result.source_opts
            this.statusOpts = res.result.status_opts
            this.adviserOpts = res.result.adviser_array
            return res.result
          })
        },
        advanced: true,
        queryParam: {},
        tabOpts: {},
        sourceOpts: {},
        firstTypeOpts: {},
        statusOpts: {},
        statusVisible: false,
        current: 0,
        statusRadio: {},
        contractForm: {},
        createValue: [],
        adviserOpts: {},
        selectedRowKeys: []
      }
    },
    methods: {
      checkContract (id) {
        const app = this
        app.current = id
        getEditContract(app.current).then(res => {
          if (res.status === 0) {
            app.statusRadio = res.result.status_items
            app.contractForm = res.result.contract
            app.statusVisible = true
          } else {
            this.$warning({
              title: res.msg
            })
          }
        })
      },
      onCheck () {
        const app = this
        const data = {}
        data['contract[status]'] = app.contractForm.status
        data['contract[notes]'] = app.contractForm.notes
        if (app.contractForm.status === '32') {
          if (app.contractForm.notes === '') {
            app.$info({
              title: '审核不通过必须填写原因'
            })
            return false
          }
          postEditContract(app.current, data).then(res => {
            if (res.status === 0) {
              app.statusVisible = false
              app.contractForm = {}
              app.$refs.table.refresh(true)
            }
          })
        } else {
          postEditContract(app.current, data).then(res => {
            if (res.status === 0) {
              app.statusVisible = false
              app.contractForm = {}
              app.$refs.table.refresh(true)
            }
          })
        }
      },
      toggleAdvanced () {
        this.advanced = !this.advanced
      },
      onChange (key) {
        this.queryParam['filter[tab]'] = key
        this.$refs.table.refresh(true)
      },
      onChange0 (date, dateString) {
        this.createValue = date
        this.queryParam['filter[begin_created_at]'] = dateString[0]
        this.queryParam['filter[end_created_at]'] = dateString[1]
      },
      onChange1 (date, dateString) {
        this.createValue = date
        this.queryParam['filter[begin_check_time]'] = dateString[0]
        this.queryParam['filter[end_check_time]'] = dateString[1]
      },
      rangeReset (clearFilters) {
        clearFilters()
        this.createValue = []
        delete this.queryParam['filter[begin_created_at]']
        delete this.queryParam['filter[end_created_at]']
        this.$refs.table.refresh(true)
      },
      handleSearch (selectedKeys, confirm, dataIndex) {
        confirm()
        this.searchText = selectedKeys[0]
        this.searchedColumn = dataIndex
        this.queryParam['filter[' + dataIndex + ']'] = selectedKeys[0]
      },
      handleReset (clearFilters, dataIndex) {
        clearFilters()
        delete this.queryParam['filter[' + dataIndex + ']']
      },
      resetSearch () {
        this.queryParam = {}
        this.$refs.table.refresh(true)
      },
      onSelectChange (selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys
      },
      filterOption (input, option) {
        return (
          option.componentOptions.children[0].text.toLowerCase().indexOf(input.toLowerCase()) >= 0
        )
      },
      copy () {
        const clipboard = new Clipboard('.copy')
        clipboard.on('success', e => {
          this.$message.success('复制成功')
          clipboard.off('error')
          clipboard.off('success')
          clipboard.destroy()
        })
        clipboard.on('error', e => {
          this.$message.error('复制失败')
          clipboard.off('error')
          clipboard.off('success')
          clipboard.destroy()
        })
      }
    }
  }
</script>

<style scoped>
  .contract_search {
    margin: 10px 0;
  }
</style>
